package com.alnex.es.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Alnex
 * @CreateTime 2020-07-20 09:30
 * @Descirption Rest 低级客户端
 */
public class TestLowESRest {
	private static final ObjectMapper MAPPEP = new ObjectMapper();

	private RestClient restClient;

	@Before
	public void init() throws Exception {
		RestClientBuilder restClientBuilder =
				RestClient.builder(new HttpHost("192.168.200.128", 9200, "http"));
		restClientBuilder.setFailureListener(new RestClient.FailureListener() {
			@Override
			public void onFailure(Node node) {
				System.out.println("出错了 " + node);
			}
		});
		this.restClient = restClientBuilder.build();
	}

	@After
	public void after() throws Exception {
		restClient.close();
	}

	// 新增数据
	@Test
	public void testCreateData() throws IOException {
		Request request = new Request("POST", "/haoke/houst");

		Map<String, Object> map = new HashMap<>();
		map.put("id","1002");
		map.put("title","江苏南京");
		map.put("price","3000");

		request.setJsonEntity(MAPPEP.writeValueAsString(map));
		Response response = this.restClient.performRequest(request);

		System.out.println(response.getStatusLine());
		System.out.println(EntityUtils.toString(response.getEntity()));
	}

	// 根据id 查询数据
	@Test
	public void testQueryDada() throws IOException {
		Request request = new Request("GET", "/haoke/houst/AXNp-MG6algCgar5_a_8");

		Response response = this.restClient.performRequest(request);

		System.out.println(response.getStatusLine());
		System.out.println(EntityUtils.toString(response.getEntity()));
	}

	//搜索数据
	@Test
	public void testSearchData() throws IOException {
		Request request = new Request("POST", "/haoke/houst/_search");
		String query = "{\"query\":{\"match\":{\"title\":\"江苏南京\"}}}";

		request.setJsonEntity(query);
		request.addParameter("pretty","true");

		Response response = this.restClient.performRequest(request);

		System.out.println(response.getStatusLine());
		System.out.println(EntityUtils.toString(response.getEntity()));
	}

	/**
	 * 批量导入本地数据
	 */
	@Test
	public void testBulks() throws IOException {
		Request request = new Request("POST", "/haoke/houst/_bulk");

		List<String> lines = FileUtils.readLines(new File("E:\\code\\data.json"), "UTF-8");

		String createStr = "{\"index\":{\"_index\":\"haoke\",\"_type\":\"houst\"}}";
		StringBuilder sb = new StringBuilder();

		int count = 0;
		for (String line : lines) {
			sb.append(createStr + "\n" + line + "\n");

			if (count >= 10) {
				request.setJsonEntity(sb.toString());
				Response response = this.restClient.performRequest(request);
				System.out.println("请求完成 -> " + response.getStatusLine());
				System.out.println(EntityUtils.toString(response.getEntity()));
				count = 0;
				sb = new StringBuilder();
			}
			count++;
		}
	}
}























